// Package common
// @Link  https://github.com/bufanyun/hotgo
// @Copyright  Copyright (c) 2023 HotGo CLI
// @Author  Ms <133814250@qq.com>
// @License  https://github.com/bufanyun/hotgo/blob/master/LICENSE
package common

import (
	"context"
	"encoding/json"
	"fmt"
	"hotgo/api/admin/common"
	"hotgo/internal/service"
	"io/ioutil"
	"net/http"
)

var (
	Map = cMap{}
)

type cMap struct{}

func (c *cMap) Map(ctx context.Context, req *common.MapReq) (res *common.MapRes, err error) {

	res = &common.MapRes{}

	config, err := service.SysConfig().GetGeo(ctx)
	if err != nil {
		return
	}

	// 构建请求URL，这里假设你需要传递一些查询参数，例如地址
	url := fmt.Sprintf("https://apis.map.qq.com/ws/geocoder/v1?location=%s&key=%s", req.Location, config.GeoTencentWebKey)

	// 发送HTTP GET请求
	httpResp, err := http.Get(url)
	if err != nil {
		return nil, err
	}
	defer httpResp.Body.Close()

	// 读取响应体
	body, err := ioutil.ReadAll(httpResp.Body)
	if err != nil {
		return nil, err
	}

	// 将响应体解析为MapRes结构体，这里假设common.MapRes是一个可以解析JSON的结构体
	err = json.Unmarshal(body, &res.MapModel)
	if err != nil {
		return nil, err
	}

	return res, nil
}
